package com.shark.manager.system.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shark.commons.base.exception.AppException;
import com.shark.commons.base.web.AjaxResult;
import com.shark.commons.base.web.BaseController;
import com.shark.commons.base.web.dto.DataGrid;
import com.shark.manager.system.entity.User;
import com.shark.manager.system.service.UserService;

@Controller
@RequestMapping("/manager/system/user")
public class UserController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(UserController.class) ;
	
	@Autowired
	private UserService service ;
	
	/**
	 * 主页
	 * @return
	 */
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main(Model model) {
		return viewName("main") ;
	}
	
	/**
	 * 表单页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Long id, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("form") ;
	}
	
	/**
	 * 添加
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public @ResponseBody AjaxResult<Object> add(User entity) {
		try {
			this.service.insertSelective(entity) ;
			return  AjaxResult.success("添加成功") ;
		} catch (AppException e) {
			return AjaxResult.failed(e.getMessage()) ;
		}
	}
	
	/**
	 * 删除
	 * @param ids 数组
	 * @return
	 */
	@RequestMapping(value="/deleteBatch", method=RequestMethod.POST)
	public @ResponseBody AjaxResult<Object> deleteBatch(Long[] ids) {
		try {
			return this.service.deleteBatchByKeys(ids) ;
		} catch (AppException e) {
			return AjaxResult.failed(e.getMessage()) ;
		}
	}
	
	/**
	 * 修改
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public @ResponseBody AjaxResult<Object> update(User entity) {
		try {
			this.service.updateByPrimaryKeySelective(entity) ;
			return  AjaxResult.success("修改成功") ;
		} catch (AppException e) {
			return AjaxResult.failed(e.getMessage()) ;
		}
	}
	
	/**
	 * 根据ID获取数据
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
	public @ResponseBody AjaxResult<User> get(@PathVariable Long id) {
		AjaxResult<User> result = null ;
		try {
			User entity = this.service.selectByPrimaryKey(id) ;
			result = new AjaxResult<User>(true, entity) ;
		} catch (AppException e) {
			logger.error(e.getMessage(), e);
			result = new AjaxResult<>(false, e.getMessage()) ;
		}
		return result ;
	}
	
	/**
	 * 分页列表
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/datagrid", method=RequestMethod.GET)
	public @ResponseBody DataGrid<User> datagrid(User dto) throws AppException {
		try {
			return this.service.datagrid(dto) ;
		} catch (AppException e) {
			throw e;
		}
	}
	
	/**
	 * 重置密码窗口
	 * @return
	 */
	@RequestMapping(value="/resetPassword_dialog", method=RequestMethod.GET)
	public String resetPassword_dialog() {
		return viewName("resetPassword_dialog") ;
	}
	
	/**
	 * 重置密码
	 * @param password
	 * @param user_ids
	 * @return
	 */
	@RequestMapping(value="/resetPassword", method=RequestMethod.POST)
	public @ResponseBody AjaxResult<Object> resetPassword(String password, Long[] ids) {
		return this.service.resetPassword(password, ids) ;
	}

}
